package com.jd.learn.util;

import com.ibm.etcd.api.Event;
import com.ibm.etcd.client.kv.KvClient;
import com.ibm.etcd.client.kv.WatchUpdate;
import com.jd.learn.config.ConfigConstant;
import com.jd.learn.configcenter.IConfigCenter;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 使用示例
 * @author shenkaiwen5
 * @version 1.0
 * @date 2020/12/22
 */
@Component
public class ConfigCenterUtil {

    @Resource
    private IConfigCenter iConfigCenter;

    @Async
    public void doTest() throws Exception {
        //上传自己的ip到配置中心
        //uploadNodeInfo();

        iConfigCenter.put("a", "111");
        iConfigCenter.putAndGrant("aa", "222", 3);

        System.out.println(iConfigCenter.get("b"));
        System.out.println(iConfigCenter.getPrefix("a").size());

        long id = iConfigCenter.keepAlive("a", "bb", 4, 5);

        KvClient.WatchIterator watchIterator = iConfigCenter.watch(ConfigConstant.rulePath);
        while (watchIterator.hasNext()) {
            WatchUpdate watchUpdate = watchIterator.next();
            List<Event> eventList = watchUpdate.getEvents();
            System.err.println(eventList.get(0).getKv());
        }
    }
}
